package com.yiqi.liebang.feature.setting.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.HelpBo;
import com.yiqi.liebang.feature.setting.view.adapter.ExpandableItemAdapter;
import com.yiqi.liebang.framework.api.HomeApi;

import java.util.ArrayList;
import java.util.List;

import javax.xml.transform.sax.TemplatesHandler;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class UsingHelpActivity extends BaseActivity {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    ArrayList<MultiItemEntity> list;
    @BindView(R.id.rv_list)
    RecyclerView mRvList;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_using_help;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "使用帮助", true, true);
        mRvList.setLayoutManager(new LinearLayoutManager(this));

    }

    @Override
    protected void initData() {
        AM.api().createApiService(HomeApi.class)
                .getHelpList()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<List<HelpBo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading(d);
                    }

                    @Override
                    public void onNext(List<HelpBo> helpBos) {
                        dismissLoading();

                        ExpandableItemAdapter adapter = new ExpandableItemAdapter(generateData(helpBos));
                        try {
                            adapter.expand(0);
                        } catch (Exception e) {

                        }


                        mRvList.setAdapter(adapter);
//

                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });
    }

    private ArrayList<MultiItemEntity> generateData(List<HelpBo> helpBos) {
        ArrayList<MultiItemEntity> res = new ArrayList<>();
        for (int i = 0; i < helpBos.size(); i++) {
            HelpBo lv0 = helpBos.get(i);
            for (int j = 0; j < lv0.getHelp().size(); j++) {
                HelpBo.HelpBean lv1 = lv0.getHelp().get(j);
                for (int k = 0; k < 1; k++) {
                    HelpBo.HelpBean.ContentBean contentBean = new HelpBo.HelpBean.ContentBean(TextUtils.isEmpty(lv1.getHelpanswers()) ? "" : lv1.getHelpanswers());
                    lv1.addSubItem(contentBean);
                }
                lv0.addSubItem(lv1);
            }
            res.add(lv0);
        }
        return res;
    }
}
